package com.qust.EnDecryption.util.net;

import com.qust.EnDecryption.util.net.status.GetState;
import com.qust.EnDecryption.util.net.status.HttpState;
import com.qust.EnDecryption.util.net.typeConversation.FastJson2PoJo;
import com.qust.agreement.result.Result;

import java.util.Map;

//@Component("netTool")
public class NetTool implements IRequest{
    private HttpState state;

    public NetTool(){
        this.state = new GetState();
    }

    /**
     * 发送网络请求
     * @param Url 网站
     * @param state 请求方式,比如GET,POST;枚举类为HttpMethod
     * @param head 请求头
     * @param pathValue 路径值,比如?id=1,map的key为参数.value为值
     * @param body 请求体,会转为json格式发送
     * @param <T> pojo.class
     * @return Result模板,data属性为pojo类型的实例或者为ArrayList泛型为pojo
     */
    @Override
    public <T> Result<?> request(
            String Url,
            HState state,
            Map<String, String> head,
            Map<String ,String > pathValue,
            T body
    ){

        return this.state.handle(Url, state, head, pathValue, body, this);
    }

    public <T> Result<?> request(
            String url,
            HState state
    ){
        return this.request(url,state,null,null,null);
    }
    

    public <T> Result<?> request(
            String Url,
            HState state,
            Map<String, String> head,
            Map<String ,String > pathValue,
            Object body,
            Class<T> type
    ){
        FastJson2PoJo json2PoJo = new FastJson2PoJo();

        return json2PoJo.conversation(
                type,
                this.request(
                      Url,
                      state,
                      head,
                      pathValue,
                      body
                )
        );
    }


    public void setState(HttpState state){
        this.state = state;
    }
}
